package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.reqeust.NewsTypeReq;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface NewsTypeMapper {
    /**
     * 条件查询文章列表顶层信息（父id=0）
     */
    @Select({"<script> " +
            " select * from cdz_system_type "+
            " <where>" +
            "and pid=0 and is_valid = 1"+
            " <if test='type != null '> " +
            " and type = #{type} "+
            " </if>"+
            " <if test='name != null '> " +
            " and name like '%${name}%' "+
            " </if> "+
            " <if test = 'beginTime != null and endTime != null ' > " +
            " and create_time BETWEEN '${beginTime} 00:00:00' and '${endTime} 23:59:59' " +
            " </if>" +
            " </where>"+
            " </script>"
    })
    List<NewsTypeReq> getList(@Param("name") String name,@Param("type") Integer type, @Param("beginTime") String beginTime,@Param("endTime") String endTime);
    /**
     * 条件查询文章列表顶层信息（父id=0）
     */
    @Select({"<script> " +
            " select * from cdz_system_type "+
            " <where>"+
            " and is_valid = 1 " +
            " <if test='type != null '> " +
            " and type = #{type} "+
            " </if>"+
            " <if test='id != null '> " +
            " and pid = #{id} "+
            " </if>"+
            " <if test='name != null '> " +
            " and name like '%${name}%' "+
            " </if>"+
            " <if test = 'beginTime != null and endTime != null ' >" +
            " and create_time BETWEEN '${beginTime} 00:00:00' and '${endTime} 23:59:59' " +
            " </if>" +
            "order by create_time desc"+
            " </where>"+
            " </script>"
    })
    List<NewsTypeReq> getListByPid(@Param("id") Integer id,@Param("name") String name,@Param("type") Integer type, @Param("beginTime") String beginTime,@Param("endTime") String endTime,
                                   @Param("startIndex") Integer startIndex, @Param("limit") Integer limit);

    /**
     * 根据父id条件查询文章列表非顶层信息
     */
    @Select("select pid from cdz_system_type where id =#{id}")
    Integer getPidById(@Param("id") Integer id);

    /**
     * 根据传入id删除非顶层信息
     */
    @Update("<script>"+
            "update cdz_system_type set is_valid = 0 where id = #{id}"+
            "</script>")
    void deleteById(@Param("id") Integer id);

    /**
     * 新增文章非顶层信息
     */
    @Insert("insert into cdz_system_type(name,type,pid,is_valid) values (#{name},#{type},#{pid},#{isVaild})")
    void createType(@Param("name") String name,@Param("type")Integer type,@Param("pid") Integer pid, @Param("isVaild")Integer isVaild);

    /**
     * 修改文章非顶层信息
     */
    @Update(" <script>"+
            " update cdz_system_type " +
            " <set>"+
            " <if test=' name != null' > "+
            " name=#{name},"+
            " </if>"+
            " <if test=' type != null' > "+
            " type=#{type},"+
            " </if>"+
            " <if test=' pid != null' > "+
            " pid=#{pid}"+
            " </if>"+
            " </set>"+
            " where id = #{id}"+
            " </script>")
    void updateType(@Param("id")Integer id,@Param("name") String name,@Param("type") Integer type,@Param("pid") Integer pid);
}
